#!/usr/bin/perl
# get_pinkey.cgi
# Attempt to start the netebbms processes

require '../netebbms-lib.pl';
use Net::Twitter::Lite;

# read argument in %in
&ReadParse();

&error_setup("<blink><font color=red>$text{'eacl_aviol'}</font></blink>");  # need check!
&error("$text{'eacl_np'} $text{'eacl_papply'}") unless $access{'apply'};  # need check!


# check argument
&error_setup($text{'start_err'});  # need check!

if (length($in{'consumerkey'}) != 21) {
	&error($text{'twitterclient_consumerkey_emsg'});
}
if (length($in{'consumersecret'}) != 41) {
	&error($text{'twitterclient_consumersecret_emsg'});
}


# twitter authorization
my %consumer_tokens = (
	consumer_key    => $in{'consumerkey'},
	consumer_secret => $in{'consumersecret'},
);

my $nt = Net::Twitter::Lite->new(%consumer_tokens);

$nt->request_token($in{'requesttoken'});
$nt->request_token_secret($in{'requestsecret'});

my @access_tokens = ( );
eval {
	@access_tokens = $nt->request_access_token(verifier => $in{'pin'});
};
if ($@) {
	&error_setup($text{'start_err'});
	&error($text{'twitterclient_accesstoken_error'});
}


# show header
&ui_print_header(undef, $text{'twitterclient_title'}, '', undef, 1, 1, undef, undef, << "HTML"
<script language=JavaScript type="text/javascript">
opener.document.configform.accesstokenkey.value = "$access_tokens[0]";
opener.document.configform.accesstokensecret.value = "$access_tokens[1]";
</script>
HTML
);


print "<p>$text{'twitterclient_accesstoken_success'}</p>";


# show footer
&ui_print_footer();

